/*
 * Copyright (c) 2014. Lincong All rights reserved.
 * Mail lincong1987@gmail.com
 * QQ 159257119
 * This software is the confidential and proprietary information of Lincong,
 * You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with Lincong.
 * $_$Id$_$
 */

/*
 * My97 DatePicker 4.8 Beta4
 * License: http://www.my97.net/dp/license.asp
 */

var $_$dp, WdatePicker;
var $_$dp_calendarPath = fb.data.base + "resource/fb-form/datepicker/calendar.min.js";
var $_$dp_langPath = fb.data.base + "resource/fb-form/datepicker/lang/{name}.min.js";
var $_$dp_skinPath = fb.data.base + "resource/fb-form/datepicker/skin/{name}/datepicker.css";


var dp = (function () {

	var showStatus = true;

	var $_$ = {
		$_$langList: [
			{name: "en", charset: "UTF-8"},
			{name: "zh-cn", charset: "gb2312"},
			{name: "zh-tw", charset: "GBK"}
		],
		$_$skinList: [
			{name: "default", charset: "gb2312"},
			{name: "whyGreen", charset: "gb2312"},
			{name: "blue", charset: "gb2312"},
			{name: "green", charset: "gb2312"},
			{name: "simple", charset: "gb2312"},
			{name: "ext", charset: "gb2312"},
			{name: "blueFresh", charset: "gb2312"},
			{name: "twoer", charset: "gb2312"},
			{name: "YcloudRed", charset: "gb2312"}
		],
		$_$wdate: true,
		$_$crossFrame: false,
		$_$preLoad: false,
		$_$dpPath: "",
		doubleCalendar: false,
		enableKeyboard: true,
		enableInputMask: true,
		autoUpdateOnChanged: null,
		weekMethod: "ISO8601",
		position: {},
		lang: "zh-cn",
		skin: "twoer",
		dateFmt: "yyyy-MM-dd",
		realDateFmt: "yyyy-MM-dd",
		realTimeFmt: "HH:mm:ss",
		realFullFmt: "%Date %Time",
		minDate: "1900-01-01 00:00:00",
		maxDate: "2099-12-31 23:59:59",
		startDate: "",
		alwaysUseStartDate: false,
		yearOffset: 1911,
		firstDayOfWeek: 0,
		isShowWeek: false,
		highLineWeekDay: true,
		isShowClear: true,
		isShowToday: true,
		isShowOK: true,
		isShowOthers: true,
		readOnly: false,
		errDealMode: 0,
		autoPickDate: null,
		qsEnabled: true,
		autoShowQS: false,
		opposite: false,
		hmsMenuCfg: {H: [1, 6], m: [5, 6], s: [15, 4]},
		specialDates: null,
		specialDays: null,
		disabledDates: null,
		disabledDays: null,
		onpicking: null,
		onpicked: null,
		onclearing: null,
		oncleared: null,
		ychanging: null,
		ychanged: null,
		Mchanging: null,
		Mchanged: null,
		dchanging: null,
		dchanged: null,
		Hchanging: null,
		Hchanged: null,
		mchanging: null,
		mchanged: null,
		schanging: null,
		schanged: null,
		eCont: null,
		vel: null,
		elProp: "",
		errMsg: "",
		quickSel: [],
		has: {},
		getRealLang: function () {
			var _ = $_$.$_$langList;
			for (var A = 0; A < _.length; A++) if (_[A].name == this.lang) return _[A];
			return _[0]
		}
	};
	var WdatePicker = U;
	var Y = window,
		T = {innerHTML: ""},
		N = "document",
		H = "documentElement",
		C = "getElementsByTagName",
		V,
		A,
		S,
		G,
		c,
		X = navigator.appName;
	if (X == "Microsoft Internet Explorer") S = true; else if (X == "Opera") c = true; else G = true;
	A = $_$.$_$dpPath || J();
//        if ($_$.$_$wdate)K(A + "skin/twoer/datepicker.css");
	V = Y;
	if ($_$.$_$crossFrame) {
		try {
			while (V.parent != V && V.parent[N][C]("frameset").length == 0) V = V.parent
		} catch (O) {
		}
	}
	if (!V.$_$dp) {
		V.$_$dp = {ff: G, ie: S, opera: c, status: 0, defMinDate: $_$.minDate, defMaxDate: $_$.maxDate};
	}
	B();
	if ($_$.$_$preLoad && $_$dp.status == 0) E(Y, "onload", function () {
		U(null, true)
	});
	if (!Y[N].docMD) {
		E(Y[N], "onmousedown", D, true);
		Y[N].docMD = true
	}
	if (!V[N].docMD) {
		E(V[N], "onmousedown", D, true);
		V[N].docMD = true
	}


	E(Y, "onunload", function () {
		if ($_$dp.dd) {
			P($_$dp.dd, "none");
		}
	});

	function B() {
		try {
			V[N], V.$_$dp = V.$_$dp || {}
		} catch ($_$) {
			V = Y;
			$_$dp = $_$dp || {}
		}
		var A = {
			win: Y,
			$_$: function (el) {
				return (typeof el == "string") ? Y[N].getElementById(el) : el;
			},
			$_$D: function ($_$, _) {
				return this.$_$DV(this.$_$($_$) && this.$_$($_$).value, _);
			},
			$_$DV: function (_, $_$) {
				if (_ != "") {
					this.dt = $_$dp.cal.splitDate(_, $_$dp.cal.dateFmt);
					if ($_$) for (var B in $_$) if (this.dt[B] === undefined) this.errMsg = "invalid property:" + B; else {
						this.dt[B] += $_$[B];
						if (B == "M") {
							var C = $_$["M"] > 0 ? 1 : 0,
								A = new Date(this.dt["y"], this.dt["M"], 0).getDate();
							this.dt["d"] = Math.min(A + C, this.dt["d"])
						}
					}
					if (this.dt.refresh()) {
						return this.dt
					}
				}
				return ""
			},
			show: function () {
				//console.log("show")
				if (showStatus === false) {
					return;
				}
				var A = V[N].getElementsByTagName("div"), zIndex = 100000;
				for (var B = 0; B < A.length; B++) {
					var _ = parseInt(A[B].style.zIndex);
					if (_ > zIndex) {
						zIndex = _;
					}
				}


				this.dd.style.zIndex = zIndex + 2;


				P(this.dd, "block");
				P(this.dd.firstChild, "");
				this.dd.style.opacity = "1";

			},
			setShowStatus: function (status) {
				showStatus = !!status;
				console.log("setShowStatus", showStatus)
			},

			unbind: function ($_$) {
				console.log("unbind")
				$_$ = this.$_$($_$);
				if ($_$.initcfg) {
					L($_$, "onclick", function () {
						U($_$.initcfg)
					});
					L($_$, "onfocus", function () {
						U($_$.initcfg)
					})
				}
			},
			hide: function () {
				P(this.dd, "none");
				this.dd.style.opacity = "0";
			},
			attachEvent: E
		};
		for (var _ in A) {
			V.$_$dp[_] = A[_];
		}
		$_$dp = V.$_$dp;
	}

	function E(B, _, A, $_$) {
		if (B.addEventListener) {
			var C = _.replace(/on/, "");
			A._ieEmuEventHandler = function (e) {
				//console.log("_ieEmuEventHandler", e.type)
				//if(B)
				return A(e)
			};
			B.addEventListener(C, A._ieEmuEventHandler, $_$)
		} else {
			B.attachEvent(_, A)
		}
	}

	function L(A, e, _) {
		if (A.removeEventListener) {
			var B = e.replace(/on/, "");
			_._ieEmuEventHandler = function (e) {
				return _(e)
			};
			A.removeEventListener(B, _._ieEmuEventHandler, false)
		} else {
			A.detachEvent(e, _)
		}
	}

	function a(_, $_$, A) {
		if (typeof _ != typeof $_$) {
			return false;
		}
		if (typeof _ === "object") {
			if (!A) for (var B in _) {
				if (typeof $_$[B] === "undefined") {
					return false;
				}
				if (!a(_[B], $_$[B], true)) {
					return false;
				}
			}
			return true;
		} else if (typeof _ === "function" && typeof $_$ === "function") {
			return _.toString() == $_$.toString();
		} else {
			return _ == $_$;
		}
	}

	function J() {
		var _, A, $_$ = Y[N][C]("script");
		for (var B = 0; B < $_$.length; B++) {
			_ = $_$[B].getAttribute("src") || "";
			_ = _.substr(0, _.toLowerCase().indexOf("wdatepicker.js"));
			A = _.lastIndexOf("/");
			if (A > 0) _ = _.substring(0, A + 1);
			if (_) break
		}
		return _;
	}

	function K(url, title, charset) {
		var head = Y[N][C]("HEAD").item(0),
			link = Y[N].createElement("link");
		if (head) {
			link.href = url;
			link.rel = "stylesheet";
			link.type = "text/css";
			if (title) {
				link.title = title;
			}
			if (charset) {
				link.charset = charset;
			}
			head.appendChild(link);
		}
	}

	function F($_$) {
		$_$ = $_$ || V;
		var A = 0, _ = 0;
		while ($_$ != V) {
			var D = $_$.parent[N][C]("iframe");
			for (var F = 0; F < D.length; F++) {
				try {
					if (D[F].contentWindow == $_$) {
						var E = W(D[F]);
						A += E.left;
						_ += E.top;
						break
					}
				} catch (B) {
				}
			}
			$_$ = $_$.parent;
		}
		return {"leftM": A, "topM": _};
	}

	function W(G, F) {
		if (G.getBoundingClientRect) {
			return G.getBoundingClientRect();
		} else {
			var A = {
					ROOT_TAG: /^body|html$_$/i,
					OP_SCROLL: /^(?:inline|table-row)$_$/i
				},
				E = false,
				I = null,
				_ = G.offsetTop,
				H = G.offsetLeft,
				D = G.offsetWidth,
				B = G.offsetHeight,
				C = G.offsetParent;
			if (C != G) while (C) {
				H += C.offsetLeft;
				_ += C.offsetTop;
				if (R(C, "position").toLowerCase() == "fixed") E = true; else if (C.tagName.toLowerCase() == "body") I = C.ownerDocument.defaultView;
				C = C.offsetParent
			}
			C = G.parentNode;
			while (C.tagName && !A.ROOT_TAG.test(C.tagName)) {
				if (C.scrollTop || C.scrollLeft) if (!A.OP_SCROLL.test(P(C))) if (!c || C.style.overflow !== "visible") {
					H -= C.scrollLeft;
					_ -= C.scrollTop;
				}
				C = C.parentNode;
			}
			if (!E) {
				var $_$ = b(I);
				H -= $_$.left;
				_ -= $_$.top
			}
			D += H;
			B += _;
			return {"left": H, "top": _, "right": D, "bottom": B}
		}
	}

	function M($_$) {
		$_$ = $_$ || V;
		var B = $_$[N],
			A = ($_$.innerWidth) ? $_$.innerWidth : (B[H] && B[H].clientWidth) ? B[H].clientWidth : B.body.offsetWidth,
			_ = ($_$.innerHeight) ? $_$.innerHeight : (B[H] && B[H].clientHeight) ? B[H].clientHeight : B.body.offsetHeight;
		return {"width": A, "height": _}
	}

	function b($_$) {
		$_$ = $_$ || V;
		var B = $_$[N],
			A = B[H],
			_ = B.body;
		B = (A && A.scrollTop != null && (A.scrollTop > _.scrollTop || A.scrollLeft > _.scrollLeft)) ? A : _;
		return {"top": B.scrollTop, "left": B.scrollLeft};
	}

	function D($_$) {
		try {
			var _ = $_$ ? ($_$.srcElement || $_$.target) : null;
			if ($_$dp.cal && !$_$dp.eCont && $_$dp.dd && _ != $_$dp.el && $_$dp.dd.style.display == "block") {
				$_$dp.cal.close();
			}
		} catch (err) {
			console.error(err)
		}
	}

	function Z() {
		$_$dp.status = 2
	}

	var Q, _;

	function U(K, C) {
		if (!$_$dp) return;
		B();
		var L = {};
		for (var H in K) L[H] = K[H];
		for (H in $_$) if (H.substring(0, 1) != "$_$" && L[H] === undefined) L[H] = $_$[H];
		if (C) {
			if (!J()) {
				_ = _ || setInterval(function () {
					if (V[N].readyState == "complete") {
						clearInterval(_);
					}
					U(null, true)
				}, 50);
				return
			}
			if ($_$dp.status == 0) {
				$_$dp.status = 1;
				L.el = T;
				I(L, true)
			} else return
		} else if (L.eCont) {
			L.eCont = $_$dp.$_$(L.eCont);
			L.el = T;
			L.autoPickDate = true;
			L.qsEnabled = false;
			I(L)
		} else {
			if ($_$.$_$preLoad && $_$dp.status != 2) return;
			var F = D();
			if (Y.event === F || F) {
				if (!F) {
					return;
				}
				L.srcEl = F.srcElement || F.target;
				F.cancelBubble = true;
			}
			L.el = L.el = $_$dp.$_$(L.el || L.srcEl);
			if (!L.el || L.el["My97Mark"] === true || L.el.disabled || ($_$dp.dd && P($_$dp.dd) != "none" && $_$dp.dd.style.left != "-970px")) {
				try {
					if (L.el["My97Mark"]) {
						L.el["My97Mark"] = false;
					}
				} catch (A) {
				}
				return
			}
			if (F && L.el.nodeType == 1 && !a(L.el.initcfg, K)) {
				$_$dp.unbind(L.el);
				E(L.el, F.type == "focus" ? "onclick" : "onfocus", function () {
					U(K)
				});
				L.el.initcfg = K;
			}
			I(L);


		}

		function J() {
			if (S && V != Y && V[N].readyState != "complete") return false;
			return true
		}

		function D() {
			if (G) {
				func = D.caller;
				while (func != null) {
					var arg = func.arguments[0];
					if (arg && (arg + "").indexOf("Event") >= 0) return arg;
					func = func.caller
				}
				return null
			}
			return event
		}
	}

	function R(_, $_$) {
		return _.currentStyle ? _.currentStyle[$_$] : document.defaultView.getComputedStyle(_, false)[$_$]
	}

	function P(_, display) {
		if (_) if (display != null) _.style.display = display; else return R(_, "display")
	}

	function I(G, _) {
		var D = G.el ? G.el.nodeName : "INPUT";
		if (_ || G.eCont || new RegExp(/input|textarea|div|span|p|a/ig).test(D)) G.elProp = D == "INPUT" ? "value" : "innerHTML"; else return;
		if (G.lang == "auto") G.lang = S ? navigator.browserLanguage.toLowerCase() : navigator.language.toLowerCase();
		if (!G.eCont) for (var C in G) $_$dp[C] = G[C];
		if (!$_$dp.dd || G.eCont || ($_$dp.dd && (G.getRealLang().name != $_$dp.dd.lang || G.skin != $_$dp.dd.skin))) {
			if (G.eCont) E(G.eCont, G); else {
				$_$dp.dd = V[N].createElement("DIV");
				$_$dp.dd.style.cssText = "position:absolute";
				
				V[N].body.appendChild($_$dp.dd);

				$_$dp.dd.style.transition = "all .3s cubic-bezier(.645, .045, .355, 1)";
				$_$dp.dd.style.opacity = "0";

				E($_$dp.dd, G);
				if (_) {
					$_$dp.dd.style.left = $_$dp.dd.style.top = "-970px";
				} else {
					$_$dp.show();
					B($_$dp);
				}
			}
		} else {
			if ($_$dp.cal) {
				$_$dp.show();
				$_$dp.cal.init();
				if (!$_$dp.eCont) {
					B($_$dp);
				}
			}
		}

		function E(K, J) {
			var I = V[N].domain,
				F = false,
				G = "<iframe style='border-radius: 3px;box-shadow: #E2E2E2 1px 1px 3px;' hideFocus=true width=9 height=7 frameborder=0 border=0 scrolling=no src=\"about:blank\"></iframe>";
			K.innerHTML = G;
			var _ = $_$.$_$langList, D = $_$.$_$skinList, H;
			try {
				H = K.lastChild.contentWindow[N]
			} catch (E) {
				F = true;
				K.removeChild(K.lastChild);
				var L = V[N].createElement("iframe");
				L.hideFocus = true;
				L.frameBorder = 0;
				L.scrolling = "no";
				L.src = "javascript:(function(){var d=document;d.open();d.domain='" + I + "';})()";
				K.appendChild(L);
				setTimeout(function () {
					H = K.lastChild.contentWindow[N];
					C()
				}, 97);
				return
			}
			C();

			function C() {
				var _ = J.getRealLang();
				//firebird.info("_____",_);
				K.lang = _.name;
				K.skin = J.skin;
				//firebird.info("KKKKK",K);
				var $_$ = ["<head><script>", "", "var doc=document, $_$d, $dp, $_$dp, $_$cfg=doc.cfg, $_$pdp = parent.$_$dp, $_$dt, $_$tdt, $_$sdt, $_$lastInput, $_$IE=$_$pdp.ie, $_$FF = $_$pdp.ff,$_$OPERA=$_$pdp.opera, $_$ny, $_$cMark = false;", "if($_$cfg.eCont){$_$dp = $dp = {};for(var p in $_$pdp)$_$dp[p]=$_$pdp[p];}else{$_$dp=$_$pdp;};for(var p in $_$cfg){$_$dp[p]=$_$cfg[p];}", "doc.oncontextmenu=function(){try{$_$c._fillQS(!$_$dp.has.d,1);showB($_$d.qsDivSel);}catch(e){};return false;};", "</script><script src=", $_$dp_langPath.replace(/\{name\}/, _.name), " charset=", _.charset, "></script>"];
				if (F) $_$[1] = "document.domain=\"" + I + "\";";
				for (var C = 0; C < D.length; C++) if (D[C].name == J.skin) $_$.push("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + $_$dp_skinPath.replace(/\{name\}/, D[C].name) + "\" charset=\"" + D[C].charset + "\"/>");
				$_$.push("<script src=\"" + $_$dp_calendarPath + "\"></script>");
				$_$.push("</head><body leftmargin=\"0\" topmargin=\"0\" tabindex=0></body></html>");
				$_$.push("<script>var t; t=t||setInterval(function(){if(doc.ready){new My97DP();$_$cfg.onload();$_$c.autoSize();$_$cfg.setPos($_$dp);clearInterval(t);}},20);</script>");
				J.setPos = B;
				J.onload = Z;
				H.write("<html>");
				H.cfg = J;
				H.write($_$.join(""));
				H.close()
			}
		}

		function B(J) {
			var H = J.position.left, C = J.position.top, D = J.el;
			if (D == T) return;
			if (D != J.srcEl && (P(D) == "none" || D.type == "hidden")) D = J.srcEl;
			var I = W(D), $_$ = F(Y), E = M(V), B = b(V), G = $_$dp.dd.offsetHeight, A = $_$dp.dd.offsetWidth;
			if (isNaN(C)) C = 0;
			if (($_$.topM + I.bottom + G > E.height) && ($_$.topM + I.top - G > 0)) C += B.top + $_$.topM + I.top - G - 2; else {
				C += B.top + $_$.topM + I.bottom;
				var _ = C - B.top + G - E.height;
				if (_ > 0) C -= _
			}
			if (isNaN(H)) H = 0;
			H += B.left + Math.min($_$.leftM + I.left, E.width - A - 5) - (S ? 2 : 0);
			J.dd.style.top = C + "px";
			J.dd.style.left = H + "px";
		}
	}

	window.$dp = window.$_$dp = $_$dp;

	$dp.$D = $_$dp.$_$D;

	window.WdatePicker = WdatePicker;

	return {
		$_$dp: $_$dp,
		WdatePicker: WdatePicker
	};
})();

// 以下配置请勿轻易改动

window.$dp = window.$_$dp = dp.$_$dp;
window.WdatePicker = dp.WdatePicker;

if (typeof define == "undefined") {

} else {
	define(function (require, exports, module) {
		/**
		 * datepicker
		 * jQuery插件包装
		 * @param options
		 * @returns {*}
		 */
		$.fn.wdatepicker = function (options) {
			options = $.extend({}, {
				on: "focus click"
			}, options);
			return this.each(function () {
				$(this).on(options.on, function () {
					window.WdatePicker.apply(this, [$.extend({}, options || {})]);
				});
			});
		};

		module.exports = window.WdatePicker;
	});
}
